#include "volumetool.h"
#include "ui_volumetool.h"
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include <QPainter>
#include <QBrush>

VolumeTool::VolumeTool(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::VolumeTool)
    , silence_flag(false)
    ,volum(20)
{
    ui->setupUi(this);

    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground,true);

    //添加自定义阴影效果
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setColor("rgb(646464)");
    shadowEffect->setOffset(0,0);
    setGraphicsEffect(shadowEffect);

    //添加静音图片
    ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));

    //默认音量20%
    ui->volumeRatio->setText("20%");
    QRect rect = ui->outLine->geometry();
    ui->outLine->setGeometry(rect.x(),205-36,rect.width(),36);
    ui->silderBtn->move(ui->silderBtn->x(),ui->outLine->y()-ui->silderBtn->height()/2);

    //安装事件监听器
    ui->volumeBox->installEventFilter(this);

}

VolumeTool::~VolumeTool()
{
    delete ui;
}


void VolumeTool::paintEvent(QPaintEvent *event)
{
    //绘制volumeTool界面向下的三角形
    QPainter painter(this);
    //设置画笔
    painter.setPen(Qt::NoPen);
    //设置画刷
    painter.setBrush(QBrush(Qt::white));
    //绘制三角形
    QPolygon polygon;
    QPoint a(20,300);
    QPoint b(80,300);
    QPoint c(50,320);
    polygon.append(a);
    polygon.append(b);
    polygon.append(c);

    painter.drawPolygon(polygon);
}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if(ui->volumeBox == watched)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            //鼠标按下事件
            computeVolume();
            return true;
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            //鼠标释放事件
            emit setMusicVolume(volum);
            return true;
        }
        else if(event->type() == QEvent::MouseMove)
        {
            //鼠标移动事件
            computeVolume();
            emit setMusicVolume(volum);
            return true;
        }
    }
    return QObject::eventFilter(watched,event);
}

void VolumeTool::computeVolume()
{
    //1.获取鼠标点击时的y坐标
    int height = ui->volumeBox->mapFromGlobal(QCursor().pos()).y();
    //验证height合法性
    height = height<25 ? 25 : height;
    height = height>205 ? 205 : height;
    //跟新outLine大小
    ui->outLine->setGeometry(ui->outLine->x(),height,ui->outLine->width(),205-height);
    //更新sliderBtn位置
    ui->silderBtn->move(ui->silderBtn->x(),ui->outLine->y() - ui->silderBtn->height()/2);
    //计算音量大小
    this->volum = ui->outLine->height() / 180.0 * 100;
    //更新音量百分比
    ui->volumeRatio->setText(QString::number(this->volum) + "%");

    if(this->volum > 0){
        silence_flag = false;
        ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
        emit silenceBtnSignal(false);
    }else{
        silence_flag = true;
        ui->silenceBtn->setIcon(QIcon(":/images/silence.png"));
        emit silenceBtnSignal(true);
    }
}

void VolumeTool::on_silenceBtn_clicked()
{
    if(volum==0) return;

    //静音 , 切换到非静音
    if(silence_flag){
        ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    }
    //非静音,切换到静音
    else{
        ui->silenceBtn->setIcon(QIcon(":/images/silence.png"));
    }
    silence_flag = !silence_flag;
    emit silenceBtnSignal(silence_flag);
}

